package com.tc.security01.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Auther: tianchao
 * @Date: 2021/10/24 22:10
 * @Description:
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @GetMapping("/demo")
    public Object demo(HttpServletRequest request){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        HttpSession session = request.getSession();
        log.info("session: {}", session);
        if (authentication.isAuthenticated()){
            return authentication.getPrincipal();
        }
        ServerProperties serverProperties = new ServerProperties();
        return null;
    }
}
